package cn.itcast.bos.web.action.base.web.utils;

import cn.itcast.bos.domain.base.Area;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

    protected T model;

    @Override
    public T getModel() {
        return model;
    }

    public BaseAction() {
        ParameterizedType type =(ParameterizedType) this.getClass().getGenericSuperclass();
        Class<T> actualTypeArguments = (Class<T>)type.getActualTypeArguments()[0];
        try {
            model = actualTypeArguments.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    
    protected int page;
    protected int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
    protected void PagePushIntoValueStack(Page<T> pageData) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", pageData.getTotalElements());
		map.put("rows", pageData.getContent());
		ActionContext.getContext().getValueStack().push(map);
	}
    
}
